using System;
using Microsoft.SPOT;
using System.Collections;
using Microsoft.SPOT.Presentation;

namespace MicroSnake
{
    /// <summary>
    /// A singleton class to manage the screen logic
    /// </summary>
    class ScreenManager
    {
        public Window mainWindow;

        private static ScreenManager screenManager;
        private static MenuScreen menuScreen;
        private static CreditsScreen creditsScreen;
        private static OptionScreen optionsScreen;
        private static LanguageScreen languageScreen;
        private static DifficultyLevelScreen difficultyLevelScreen;
        private static SplashScreen splashScreen;
        private static GameScreen gameScreen;

        private ScreenManager(Window mainWindow)
        {
            this.mainWindow = mainWindow;
        }

        public static ScreenManager Create(Window mainWindow)
        {
            if (screenManager == null)
            {
                screenManager = new ScreenManager(mainWindow);
            }
            return screenManager;
        }

        public static ScreenManager Instance
        {
            get
            {
                return screenManager;
            }
        }

        /// <summary>
        /// Destroy all screens so that when we call MakeXXX methods all the screens
        /// will be recreated. This funcion is important to support runtime-language changes.
        /// </summary>
        public static void DestroyAllScreen()
        {
            menuScreen = null;
            creditsScreen = null;
            optionsScreen = null;
            languageScreen = null;
            difficultyLevelScreen = null;
            splashScreen = null;
        }

        // Methods used to navigate between screens

        public void ShowMenuScreen()
        {
            mainWindow.Child = MakeMenuScreen();
            menuScreen.Focus();
        }

        public void ShowCreditsScreen()
        {
            mainWindow.Child = MakeCreditsScreen();
            creditsScreen.Focus();
        }

        public void ShowOptionsScreen()
        {
            mainWindow.Child = MakeOptionsScreen();
            optionsScreen.Focus();
        }

        public void ShowLanguageScreen()
        {
            mainWindow.Child = MakeLanguageScreen();
            languageScreen.Focus();
        }

        public void ShowDifficultyLevelScreen()
        {
            mainWindow.Child = MakeDifficultyLevelScreen();
            difficultyLevelScreen.Focus();
        }

        public void ShowSplashScreen()
        {
            mainWindow.Child = MakeSplashScreen();
            splashScreen.Focus();
        }

        public void ShowGameScreen()
        {
            mainWindow.Child = MakeGameScreen();
            gameScreen.Focus();
        }

        // Factory methods to build the various screen

        public static MenuScreen MakeMenuScreen()
        {
            if (menuScreen == null)
            {
                menuScreen = new MenuScreen();
            }
            
            return menuScreen;
        }

        public static CreditsScreen MakeCreditsScreen()
        {
            if (creditsScreen == null)
            {
                creditsScreen = new CreditsScreen();
            }
            return creditsScreen;
        }

        public static OptionScreen MakeOptionsScreen()
        {
            if (optionsScreen == null)
            {
                optionsScreen = new OptionScreen();
            }
            return optionsScreen;
        }

        public static LanguageScreen MakeLanguageScreen()
        {
            if (languageScreen == null)
            {
                languageScreen = new LanguageScreen();
            }
            return languageScreen;
        }

        public static DifficultyLevelScreen MakeDifficultyLevelScreen()
        {
            if (difficultyLevelScreen == null)
            {
                difficultyLevelScreen = new DifficultyLevelScreen();
            }
            return difficultyLevelScreen;
        }

        public static SplashScreen MakeSplashScreen()
        {
            if (splashScreen == null)
            {
                splashScreen = new SplashScreen();
            }
            return splashScreen;
        }

        public static GameScreen MakeGameScreen()
        {
            if (gameScreen == null)
            {
                gameScreen = new GameScreen();
            }
            return gameScreen;
        }
    }
}
